package com.Java.train.member.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.Java.train.common.req.MemberTicketReq;
import com.Java.train.common.response.CommonResp;
import com.Java.train.member.domain.DTO.TicketDTO;
import com.Java.train.member.req.TicketQueryReq;
import com.Java.train.common.exception.BusniessException;
import com.Java.train.common.exception.BusniessExceptionEnum;
import com.Java.train.common.response.PageResp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.Java.train.member.domain.Ticket;
import com.Java.train.member.mapper.TicketMapper;
import com.Java.train.member.service.TicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.Java.train.common.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.date.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
/**
* 车票 服务实现类
*
* @author luomoxingchen
* @since 2024-10-08
*/
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {

    private static final Logger LOG = LoggerFactory.getLogger(TicketService.class);

    @Autowired
    TicketMapper ticketMapper;


    @Override
    public PageResp<TicketDTO> queryList(TicketQueryReq ticketQueryReq){
        // 1.获取当前用户id信息
        //        Long id = LoginMemberContext.getId();
        Long memberId = ticketQueryReq.getMemberId();
        // 2.查询数据库中是否存在当前信息（id）
        LOG.info("查询页码：{}", ticketQueryReq.getPage());
        LOG.info("每页条数：{}", ticketQueryReq.getSize());
        PageHelper.startPage(ticketQueryReq.getPage(),ticketQueryReq.getSize());
        List<Ticket> tickets = list();
//        if(ObjectUtil.isNotEmpty(memberId)){
//
//        }
        LambdaQueryWrapper<Ticket> wrapper = new LambdaQueryWrapper<>();
        tickets = list(wrapper.eq(Ticket::getMemberId, memberId));
        if(tickets == null || CollUtil.isEmpty(tickets)){
            LOG.info("没有查到相关车票信息");
//            throw new BusniessException(BusniessExceptionEnum.MEMBER_PASSENGER_NOT_EXITS);
        }

        // 3 分析分页信息
        // 3.1 获取分页总条数和页数
        PageInfo<Ticket> pageInfo = new PageInfo<>(tickets);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        // 3.2 转为封装后的返回值并封装到分页返回值中
        List<TicketDTO> list = BeanUtil.copyToList(tickets, TicketDTO.class);
        PageResp<TicketDTO> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        // 4.返回查询到的信息
        return pageResp;
    }

    @Override
    public CommonResp<Object> modify(MemberTicketReq req) {
        DateTime dateTime = DateTime.now();
        Ticket ticket = BeanUtil.copyProperties(req, Ticket.class);
        ticket.setId(SnowFlakeUtil.getSnowFlakeNextId());
        ticket.setCreateTime(dateTime);
        ticket.setUpdateTime(dateTime);
        boolean save = save(ticket);
        return new CommonResp<>(save);
    }

    @Override
    public CommonResp<Object> remove(String ids) {
        if(StrUtil.isNotEmpty(ids)){
            String[] array = ids.split(",");
            if(!CollectionUtils.isEmpty(Arrays.asList(array))) {
                return new CommonResp<>(ticketMapper.deleteBatchIds(Arrays.asList(array)));
            }
        }
        return new CommonResp<>(Collections.EMPTY_LIST);
    }

}


